package com.itheima.pay.controller;




import com.github.wxpay.sdk.WXPay;

import com.itheima.pay.config.WxpayConfig;


import org.springframework.web.bind.annotation.*;



import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wxpay")
public class WxpayController {

    @GetMapping("precreate/{orderNo}")
    public Map<String, String>  precreate(@PathVariable String orderNo) throws Exception{
        //初始配置
        WxpayConfig wxpayConfig = new WxpayConfig();
        WXPay wxPay = new WXPay(wxpayConfig);
        //封装参数
        Map<String, String> map = new HashMap<>();
        map.put("body","华为P99999");
        map.put("out_trade_no",orderNo);
        map.put("total_fee","1");
        map.put("spbill_create_ip","192.168.111.111");
        map.put("notify_url","www.itheima.com");
        map.put("trade_type","NATIVE");
        //执行
        Map<String, String> unifiedMap = wxPay.unifiedOrder(map);
        return unifiedMap;
    }

    @GetMapping("query/{orderNo}")
    public Map<String, String> query(@PathVariable String orderNo) throws Exception {
        WxpayConfig wxpayConfig = new WxpayConfig();
        WXPay wxPay = new WXPay(wxpayConfig);
        Map<String, String> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        Map<String, String> query = wxPay.orderQuery(map);
        return query;
    }

    @GetMapping("close/{orderNo}")
    public Map<String, String> close(@PathVariable String orderNo) throws Exception {
        WxpayConfig wxpayConfig = new WxpayConfig();
        WXPay wxPay = new WXPay(wxpayConfig);
        Map<String, String> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        Map<String, String> closeOrder = wxPay.closeOrder(map);
        return closeOrder;
    }

    @GetMapping("refund/{orderNo}/{refundNo}")
    public Map<String, String> refund(@PathVariable String orderNo,@PathVariable String refundNo) throws Exception {
        WxpayConfig wxpayConfig = new WxpayConfig();
        WXPay wxPay = new WXPay(wxpayConfig);
        Map<String, String> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        map.put("out_refund_no",refundNo);
        map.put("total_fee","1");
        map.put("refund_fee","1");

        Map<String, String> refundMap = wxPay.refund(map);
        return refundMap;
    }
    @GetMapping("refundquery/{orderNo}")
    public Map<String, String> refundquery(@PathVariable String orderNo) throws Exception {
        WxpayConfig wxpayConfig = new WxpayConfig();
        WXPay wxPay = new WXPay(wxpayConfig);
        Map<String, String> map = new HashMap<>();
        map.put("out_trade_no",orderNo);




        Map<String, String> query = wxPay.refundQuery(map);
        return query;
    }

}
